<?php
// unneeded session_start();

class Messaging_models extends CI_Model{
    
    
    function __construct()
    {
        parent::__construct();
    }
    
    
    
    
    public function send_message() {
        
        $current_turn = '1';
        
        $new_mailmsg = new ORM\Dynasties2\Mailmsgs;
        $new_mailmsg->setMsgText($this->input->post('msgbody'));
        $new_mailmsg->setFromUser($this->input->post('family_from'));
        $new_mailmsg->setTimeStamp(new DateTime());
        $new_mailmsg->setOnTurn($current_turn);
        $new_mailmsg->setImportance($this->input->post('importance'));
        $new_mailmsg->setMsgType($this->input->post('msgtype'));
        $this->doctrine->em->persist($new_mailmsg);
        $this->doctrine->em->flush();
        
        $new_maillink = new ORM\Dynasties2\Maillinks;
        $new_maillink->setMsgId($new_mailmsg);
        //$new_maillink->setMsgId('100');
        $new_maillink->setToUser($this->input->post('family_to'));
        $new_maillink->setIsRead('0');
        $this->doctrine->em->persist($new_maillink);
        $this->doctrine->em->flush();
        
        
        
    } ////////// end function send_message
    
    public function get_my_unread_messages() {
        
        //$my_family = $_SESSION['active_family'];
		$my_family = '14';
        
        //$query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Families u WHERE u.isActive = '1' OR u.isActive IS NULL");

        //$query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Maillinks u WHERE u.toUser = $my_family AND u.isRead = '0'");
        
        //$query = $this -> doctrine -> em -> createQuery ("SELECT a, b.msgText, b.fromUser, b.timeStamp, b.importance, b.msgType FROM ORM\Dynasties2\Maillinks a JOIN a.msgId b WHERE a.toUser = $my_family");

	//$query = $this -> doctrine -> em -> createQuery ("SELECT b.msgText, b.fromUser, b.timeStamp, b.importance, b.msgType FROM ORM\Dynasties2\Maillinks a JOIN a.msgId b");
	$query = $this -> doctrine -> em -> createQuery ("SELECT a, b FROM ORM\Dynasties2\Maillinks a JOIN a.msgId b");

        
        return $query->getResult();
        
    }////////// end function get_my_unread_messages
    
    
}